package com.app.fk.mensa;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import android.R.string;
import android.app.ProgressDialog;
import android.content.Context;

public class Html_encoder {
	
	private final static String url = "http://www.studentenwerk-mannheim.de/mensa/wo_hs.normal.php?+kw=";
	
	public static Menues getMenues(int week)
	{	
		if(week <= 0 || week > 52)
			return null;
			
		
		Menues result = new Menues();
		
		
		String content = getContent(url + week);
		
		
		
		if(content == null)
			return null;
		
		String []menues = newparseContent(content);
		
		if(menues == null)
			return null;
		
		if(result.setMenues(menues) == false)
			return null;
		
		return result;
		
	}
	
	
	
	
	private static String getContent(String url)  
	{  	  
		
		HttpGet get = new HttpGet(url);
		String result = "";
		
		DefaultHttpClient client = new DefaultHttpClient();
		        
		try {
				
		        HttpResponse response = client.execute(get);
		               
		        HttpEntity entity = response.getEntity();  
		        
		        if (entity != null) {  
					

		        	InputStream instream = entity.getContent();  
		        	BufferedReader reader = new BufferedReader(new InputStreamReader(instream));  
		        	StringBuilder sb = new StringBuilder();  
		        	  
		        	String line = null;  
		        	while ((line = reader.readLine()) != null)  
		        	sb.append(line + "n");  
		        	result = sb.toString();  
		        	
		        
		        }
		                        
		} catch (ClientProtocolException e) {
		        e.printStackTrace();
		        return null;
		} catch (IOException e) {
		        e.printStackTrace();
		        return null;
		}catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
		return result;
	}
	
	private static String[] newparseContent(String content)
	{
		String[] test = new String[5];
		int moIdx = content.indexOf("Montag");
		int diIdx = content.indexOf("Dienstag");
		int miIdx = content.indexOf("Mittwoch");
		int doIdx = content.indexOf("Donnerstag");
		int frIdx = content.indexOf("Freitag");
		
		test[0] = content.substring(moIdx, diIdx);
		test[1] = content.substring(diIdx, miIdx);
		test[2] = content.substring(miIdx, doIdx);
		test[3] = content.substring(doIdx, frIdx);
		test[4] = content.substring(frIdx, content.length());
		
		//Test Close
		for(int k = 0; k < 5; k++)
		{
			if(test[k].indexOf("geschlossen") > -1)
			{
				test[k] = "close";
			}
		}
		
		String[][] week = new String[5][4];
		
		week[0] = test[0].split("<h4>",4);
		week[1] = test[1].split("<h4>",4);
		week[2] = test[2].split("<h4>",4);
		week[3] = test[3].split("<h4>",4);
		week[4] = test[4].split("<h4>",4);
		
		String[] result = new String[15];
		
		for(int j = 0; j < 5; j++)
			for(int i = 1; i<=3; i++)
			{
								
				if(week[j][0] == "close")
					{
						result[j*3 + 0] = "close";
						result[j*3+1] = "close";
						result[j*3+2] = "close";
						
						
						//i = 0;
						break;
					}
				week[j][i] = week[j][i].substring(0, week[j][i].indexOf("</td>"));
				week[j][i] = week[j][i].replaceAll("</h4>", "\n");
				result[j*3 + (i-1)] = week[j][i];
			}
		
		for (int l = 0; l < 15; l++)
			{
				if (result[l] == null)
					return null;			        
			}
				
		return result;
	}
	
	
	private static String[] parseContent(String content)
	{
				
		String[] test = content.split("<h4>");
		String[] result = new String[15];
		
			
		
		for(int i = 1;i<test.length; i++)
		{
		if(test[i].indexOf("geschlossen") != -1)
		{
			//TODO: Parsen von mehreren geschlossenen Tagen
			result[i-1] = test[i].substring(0, test[i].indexOf("</td>"));
			result[i-1] = result[i-1].replaceAll("</h4>", "\n");
			
			test[i] = test[i].substring(test[i].indexOf("geschlossen")+5,test[i].length()-1);
			result[i] = "Close";
			result[i+1] = "Close";
			result[i+2] = "Close";
			i = i+4;
			continue;
		}
		result[i-1] = test[i].substring(0, test[i].indexOf("</td>"));
		result[i-1] = result[i-1].replaceAll("</h4>", "\n");
		}
		
		return result;
	}


}
